package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.DWaybillAbandonedLogService;
import cn.huiyunche.base.service.mappers.DWaybillAbandonedLogMapper;
import cn.huiyunche.base.service.mappers.ext.DWaybillAbandonedLogExtMapper;
import cn.huiyunche.base.service.model.DWaybillAbandonedLog;
import cn.huiyunche.base.service.query.DWaybillAbandonedLogConditions;
import cn.huiyunche.base.service.vo.DWaybillAbandonedLogVo;
import cn.huiyunche.base.service.vo.PageVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @FileName: cn.huiyunche.base.service.interfaces.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2017/3/23 上午9:56
 */
@Service
public class DWaybillAbandonedLogServiceImpl implements DWaybillAbandonedLogService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DWaybillAbandonedLogServiceImpl.class);

    @Autowired
    private DWaybillAbandonedLogMapper dWaybillAbandonedLogMapper;

    @Autowired
    private DWaybillAbandonedLogExtMapper dWaybillAbandonedLogExtMapper;

    @Override
    public int add(DWaybillAbandonedLog log) throws Exception {
        LOGGER.info("DWaybillAbandonedLogServiceImpl.add param log : {}", log);

        if ( null == log ) {
            LOGGER.error("DWaybillAbandonedLogServiceImpl.add param log must not be null");
            throw new IllegalArgumentException("日志信息不能为空");
        }

        return dWaybillAbandonedLogMapper.insertSelective(log);
    }

    @Override
    public Map<String, Object> selectListByConditions(PageVo pageVo, DWaybillAbandonedLogConditions conditions) {
        LOGGER.info("DWaybillAbandonedLogServiceImpl.selectListByConditions params : {}, {}", pageVo, conditions);

        if (null == conditions) {
            LOGGER.error("DWaybillAbandonedLogServiceImpl.selectListByConditions param conditions must not be null ");
            throw new IllegalArgumentException("查询条件信息不能为空");
        }

        //返回值map
        Map<String, Object> map = new HashMap<>();

        //查询参数map
        Map<String, Object> paramsMap = new HashMap<>();

//        private String orderCode;
//        //废弃原因编码
//        private String abandonedCode;
//        //废弃原因
//        private String abandonedReason;
//        //创建时间
//        private Date gmtCreate;
        if ( StringUtils.isNotBlank(conditions.getOrderCode()) ) {
            paramsMap.put("orderCode", conditions.getOrderCode() + "%" );
        }
        if ( null != conditions.getAbandonedCode() ) {
            paramsMap.put("abandonedCode", conditions.getAbandonedCode() + "%" );
        }
        if (StringUtils.isNotBlank(conditions.getAbandonedReason())) {
            paramsMap.put("abandonedReason", "%" + conditions.getAbandonedReason() + "%" );
        }
        if (null != conditions.getGmtCreate()) {
            paramsMap.put("gmtCreate", conditions.getGmtCreate());
        }

        if (null != pageVo) {
            paramsMap.put("orderByClause", StringUtils.isNotBlank(pageVo.getOrder()) == true ? pageVo.getOrder() : " da.gmt_update DESC");
            paramsMap.put("limitStart", pageVo.getStartIndex());
            paramsMap.put("limitEnd", pageVo.getPageSize());
            pageVo.setTotalRecord(dWaybillAbandonedLogExtMapper.countByExample(paramsMap));
        }

        List<DWaybillAbandonedLogVo> list = dWaybillAbandonedLogExtMapper.selectByExample(paramsMap);
        map.put("list", list);

        if (null != pageVo) {
            map.put("page", pageVo);
        }

        return map;
    }
}
